import WalletConnect from "@walletconnect/client";
import QRCodeModal from "@walletconnect/qrcode-modal";

// 创建连接
const connector = new WalletConnect({
  bridge: "https://bridge.walletconnect.org",
  qrcodeModal: QRCodeModal,
});

// 检查连接是否已经建立
if (!connector.connected) {
  // 创建会话
  connector.createSession();
}

// 订阅连接事件
connector.on("connect", (error, payload) => {
  if (error) {
    throw error;
  }
    // 连接成功后会返回钱包帐号和链ID
  const { accounts, chainId } = payload.params[0];
});

connector.on("session_update", (error, payload) => {
  if (error) {
    throw error;
  }
  const { accounts, chainId } = payload.params[0];
});

connector.on("disconnect", (error, payload) => {
  if (error) {
    throw error;
  }
});

// 调用方法， 详细介绍请访问https://docs.walletconnect.com/1.0/client-api
// 发送交易方法示例
connector.sendTransaction({
  data: "0x",
  from: "0xc115ceadf9e5923330e5f42903fe7f926dda65d2",
  gasLimit: "0x5208",
  gasPrice: "0x746a528800",
  nonce: "0x12",
  to: "0xc115ceadf9e5923330e5f42903fe7f926dda65d2",
  value: "0x00"
}).then(txHash => {
  // 发送交易成功， 钱包会返回交易hash
  console.log('txHash: ', txHash)
})
